<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>defineProperty</title>
</head>
<body>
    <div id="app">
        hello
    </div>
    <script>
        let data = {
            msg: 'hello',
            count: 10
        }

        let vm = {}
        proxyData(data)
        
        function proxyData(data) {
            Object.keys(data).forEach(key => {
                Object.defineProperty(vm, key, {
                    enumerable: true,
                    configurable: true,
                    get() {
                        console.log('get: ', key, data[key])
                        return data[key]
                    },
                    set(newValue) {
                        console.log('set: ', key, newValue)
                        if(newValue === data[key]) {
                            return
                        }
                        data[key] = newValue
                        document.querySelector('#app').textContent = data[key]
                    }
                })
            })
        }
        vm.msg = 'Hello World'
        console.log(vm.msg)
    </script>
</body>
</html>